<template>
  <div class="login_content">
     <div class="login_box">
       <!-- 头像区域 -->
       <div class="avatar_box">
         <img src="../assets/logo.png" >
       </div>
       <div class="title">
         <h1>Vue 管理系统</h1>
       </div>
       <!-- 表单区域 -->
       <el-form
         ref="formRef"
        :model="form"
        :rules="rules"
        class="login_form">
         <!-- 账号 -->
         <el-form-item prop="account">
            <el-input prefix-icon="el-icon-user" v-model="form.account"></el-input>
         </el-form-item>
         <!-- 密码 -->
         <el-form-item prop="password">
            <el-input   @keydown.enter.native="login" v-focus prefix-icon="el-icon-lock" v-model="form.password" type="password" ></el-input>
         </el-form-item>
         <!-- 按钮 -->
         <el-form-item class="btns">
           <el-button type="primary" @click="login">登入</el-button>
           <el-button type="info" @click="clearForm">重置</el-button>
         </el-form-item>
       </el-form>
     </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        account: 'admin1',
        password: ''
      },
      rules: {
        account: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  directives: {
    focus: {
      inserted: function (el) {
        el.querySelector('input').focus()
      }
    }
  },
  methods: {
    clearForm () {
      this.$refs.formRef.resetFields()
    },
    login () {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        // const res = await this.$http.post('/login', this.form)
        const { data } = await this.$http.post('/login', this.form)
        // console.log(data)
        if(data.status){
          if(data.status === 201){
            return this.$message.error(data.msg)
          }
        }
        this.$store.commit('getList', data.role)
        this.$store.commit('upInfo', data)

        // this.$store.dispatch('getInfo')

        // if (res.data.err_code !== 0) return this.$message.error('用户或密码错误！')
        this.$message.success('登入成功！')
        // console.log(res.data)
        // window.sessionStorage.setItem('token', res.data.token)
        // window.sessionStorage.setItem('user', JSON.stringify(res.data.user))
        this.$router.push('/home')
        // console.log(this.$store.state)
      })
    }
  }
}
</script>

<style  lang="less" scoped>
.login_content {
  background:url(../assets/img/login.jpg);
  height:100%;
}

.login_box {
  opacity: 0.8;
  width:450px;
  height:400px;
  background: whitesmoke;
  border-radius: 10px;
  position: absolute;
  left:50%;
  top:50%;
  transform: translate(-50%,-50%);
  .avatar_box {
    height:100px;
    width:100px;
    border: 1px solid #ddd;
    border-radius: 50%;
    padding:10px;
    background: #eee;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left:50%;
    transform: translate(-50%,-50%);
    img {
      width:100%;
      height:100%;
      border-radius: 50%;
      background: #eee;
    }
  }
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.login_form {
  width:100%;
  position: absolute;
  bottom:0;
  padding:0 20px;
  box-sizing: border-box;
}

.title {
  position: absolute;
  left:50%;
  top:25%;
  transform: translate(-50%,-25%);
  padding:5px;
}
</style>
